package com.yangcangclub.trolley.domain;

import com.google.common.collect.Maps;
import com.yangcangclub.commons.exception.ApolloException;
import com.yangcangclub.commons.result.MessageEnum;
import com.yangcangclub.trolley.base.TrolleyBaseDomain;
import com.yangcangclub.trolley.bean.ShoppingTrolley;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * Created by 肖明明 on 2017/2/27.
 */
@Component
public class TrolleyBasisDomain extends TrolleyBaseDomain {

    /**
     * 添加到购物车，带数量
     */
    public int addToTrolley(int pid, int userId, int num) {
        ShoppingTrolley st=new ShoppingTrolley();
        st.setUserId(userId);
        st.setProductId(pid);
        st.setNum(num);
        super.getMasterJdbc().insert("trolley.TrolleyMapper.insertTrolleyProduct",st);
        return st.getId();
    }

    /**根据购物车记录ID列表获取未删除的商品ID和数量列表
     * 购买时可用
     */
    public List<Map<String,Object>> getProductsListByTids(int userId,List<Integer> tids){
        Map m=Maps.newHashMap();
        m.put("userId",userId);
        m.put("tids",tids);
        return super.getMasterJdbc().selectList("trolley.TrolleyMapper.selectProductListByTids",m);
    }
    
    /**根据商品ID查询购物车添加记录,未删除记录*/
    public ShoppingTrolley selectShoppingTrolley(int userId,int pid){
        Map m= Maps.newHashMap();
        m.put("userId",userId);
        m.put("pid",pid);
        return super.getMasterJdbc().selectOne("trolley.TrolleyMapper.selectShoppingTrolleyByPid",m);
    }

    /**
     * 根据购物车ID列表批量删除购物车商品
     */
    public void delProductFromTrolley(int userId,List<Integer> tids) {
        Map m= Maps.newHashMap();
        m.put("userId",userId);
        m.put("tids",tids);
        super.getMasterJdbc().update("trolley.TrolleyMapper.deleteTrolleyProductByPIds",m);
    }

    /**
     * 查询用户未删除的购物车商品分页列表，可以用pageNum
     */
    public List<Map<String, Object>> getTrolleyProductsByPage(int userId, int start, int pageSize) {
        Map m= Maps.newHashMap();
        m.put("userId",userId);
        m.put("start",start);
        m.put("pageSize",pageSize);
        return super.getMasterJdbc().selectList("trolley.TrolleyMapper.selectTrolleyProductByPage",m);
    }

    /**
     * 查询用户未删除的购物车商品分页列表总数，可以用pageNum
     */
    public long getTrolleyProductsByPageTotalNum(int userId) {
        return super.getMasterJdbc().selectOne("trolley.TrolleyMapper.selectTrolleyProductByPageNum",userId);
    }

    /**根据购物车ID修改购物车单个商品数量*/
    public int changeNumOfProductInTrolley(int userId,int id, int newNum) {
        Map m= Maps.newHashMap();
        m.put("userId",userId);
        m.put("id",id);
        m.put("newNum",newNum);
        return super.getMasterJdbc().update("trolley.TrolleyMapper.updateTrolleyProductNum",m);
    }

    /**
     * 根据购物车ID修改购物车单个商品数量,加减一操作
     * @param id 购物车ID
     * @param step 1 or -1
     * @return
     */
    public int increaseOrDecreaseByOne(int id,int step){
        if(step!=1&&step!=-1) throw new ApolloException(MessageEnum.PARMA_FORMAT_ERROR);
        Map m= Maps.newHashMap();
        m.put("id",id);
        m.put("step",step);
        return super.getMasterJdbc().update("trolley.TrolleyMapper.updateTrolleyPNumByOne",m);
    }
    
}
